package tk.mybatis.springboot.util;

import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

public class HttpClientExample {



    public static void sendGetRequestAndParseResponse(String url) {
        HttpClient httpClient = HttpClientBuilder.create().build();

        HttpGet httpGet = new HttpGet(url);

        try {
            HttpResponse httpResponse = httpClient.execute(httpGet);

            // 检查响应状态码
            int statusCode = httpResponse.getStatusLine().getStatusCode();
            if (statusCode == 200) {
                // 解析响应内容
                String responseBody = EntityUtils.toString(httpResponse.getEntity());
                // 在此处处理解析后的响应数据
                System.out.println("响应内容: " + responseBody);
            } else {
                System.err.println("请求失败，状态码: " + statusCode);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 释放资源
            httpClient.getConnectionManager().shutdown();
        }
    }
}
